package com.xnfh.planting.ui.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.xnfh.common.bean.PlantOptionBean
import com.xnfh.common.bean.PlantOptionBeanConvert
import com.xnfh.common.bean.StandardBean
import com.xnfh.framework.utils.JsonUtlis
import com.xnfh.planting.R
import com.xnfh.planting.databinding.ItemPlantOptionLayoutBinding

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/18 09:47
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/18 09:47
 * @UpdateRemark: 更新说明
 */
class PlantOptionAdapter :
    BaseQuickAdapter<PlantOptionBeanConvert, BaseDataBindingHolder<ItemPlantOptionLayoutBinding>>(
        R.layout.item_plant_option_layout
    ) {
    private var callback: PlantOptionCallBack? = null
    override fun convert(
        holder: BaseDataBindingHolder<ItemPlantOptionLayoutBinding>,
        item: PlantOptionBeanConvert
    ) {
        val dataBinding = holder.dataBinding
        holder.setText(R.id.item_plant_option_service_name, item.name)
        holder.setText(R.id.item_plant_option_growth_name, item.growthName)
        if (!item.standard.isNullOrEmpty()) {
            val standardBeans = JsonUtlis.toArrayList(item.standard, StandardBean::class.java)
            val stringBuilder = StringBuilder()
            for ((index, standardBean) in standardBeans.withIndex()) {
                stringBuilder.append(standardBean.standard)
                //如果不是最后一条数据，就就一个换行符，最后一条数据不用加
                if (standardBeans.size - 1 != index) {
                    stringBuilder.append("\n")
                }
            }
            //设置内容展示
            holder.setText(R.id.item_plant_option_rs_value, stringBuilder.toString())
        }
        //type != 0 就是病虫害症状
        holder.setText(
            R.id.item_plant_option_rs,
            if (item.type == "0") context.getString(R.string.str_reference_standard)
            else context.getString(R.string.str_bch_zz)
        )
        holder.setText(
            R.id.item_plant_option_err_sure,
            if (item.type == "0") context.getString(R.string.str_labour_find_field)
            else context.getString(R.string.str_yj_sure)
        )
        //如果不是病虫害，并且ai = 0 就隐藏按钮
        holder.setGone(R.id.item_plant_option_err_sure, item.type == "0" || item.ai == "0")
        dataBinding?.itemPlantOptionErrSure?.setOnClickListener {
            //参数 是否是病预警
            callback?.XunTianOnClick(item.type != "0", item)
        }
        dataBinding?.itemPlantOptionReport?.setOnClickListener {
            callback?.IntServDetailOnClick(item)
        }
    }


    interface PlantOptionCallBack {
        /**
         * @param isDisease 是否是病害预警
         */
        fun XunTianOnClick(isDisease: Boolean, item: PlantOptionBeanConvert)

        fun IntServDetailOnClick(item: PlantOptionBeanConvert)
    }

    fun setPlantOptionCallBack(callback: PlantOptionCallBack) {
        this.callback = callback
    }
}